package com.hicampus.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;

import com.liferay.portal.kernel.util.HttpUtil;
import com.liferay.portal.kernel.util.StringUtil;

public class SMSUtil {
	
	private static String httpURL = "http://luna.a2wi.co.in:7501/failsafe/HttpLink?aid=[$aid$]&pin=[$pin$]&mnumber=[$mnumber$]&message=[$msgText$]";
	
	public static String push(String mobileNumber, String msgText) {
		
		String retValue = null;
		
		System.out.println(msgText);
		
		String [] oldSubs = {"[$aid$]", "[$pin$]", "[$mnumber$]", "[$msgText$]"};
		String [] newSubs = {"501550", "demo@1", mobileNumber, HttpUtil.encodeURL(msgText)};
		
		httpURL = StringUtil.replace(httpURL, oldSubs, newSubs);
		
		System.out.println("httpURL ==> " + httpURL);
		
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod(httpURL);
		
		try {
			client.executeMethod(method);
			
			InputStream is = method.getResponseBodyAsStream();
			
			retValue = IOUtils.toString(is);
			
			is.close();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}
		
		return retValue;
	}
}
